package com.baofu.paycore.biz.engine.processor.impl;

import com.baofu.account.service.facade.model.request.TransFerReqDTO;
import com.baofu.paycore.biz.engine.command.impl.BalanceCommand;
import com.baofu.paycore.biz.engine.processor.BaseProcessor;
import com.baofu.paycore.biz.engine.result.CommandResult;
import com.baofu.paycore.biz.enums.CommandProcessorEnum;
import com.baofu.paycore.biz.external.account.AccountService;
import com.baofu.paycore.biz.paycore.converter.BO2BOConverter;
import com.baofu.paycore.service.facade.enums.PayCoreErrorCode;
import com.baofu.paycore.manager.model.AccountRespBO;
import com.baofu.paycore.service.facade.enums.PayStatusEnums;
import com.baofu.paycore.service.facade.enums.RequestSystemEnum;
import com.google.common.collect.Maps;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * 余额处理器
 * <p>
 *  1、获取处理器类型
 *  2、执行特定支付工具的特定支付类型的批量支付命令，并返回相应的结果
 *  3、转账务转账参数
 * </p>
 * User: LZQ  Date: 2014/12/19 ProjectName: paycore Version: 5.0.0
 */
@Slf4j
@Service
public class BalanceProcessor implements BaseProcessor<BalanceCommand> {

    /** 账务转账服务 */
    @Autowired
    private AccountService accountService;

    /**
     * 获取处理器类型
     *
     * @return  处理器类型
     */
    @Override
    public CommandProcessorEnum getProcessorType() {
        return CommandProcessorEnum.BALANCE_PROCESSOR;
    }

    /**
     * 执行特定支付工具的特定支付类型的批量支付命令，并返回相应的结果
     *
     * @param  balanceCommand  支付命令
     * @return Map
     */
    @Override
    public Map<String, CommandResult> doProcess(BalanceCommand balanceCommand) {
        log.debug("进入余额类支付统一处理器：commands={}", balanceCommand);
        Map<String, CommandResult> result = Maps.newHashMap();
        try {
            // 指令转换成账务请求对象
            TransFerReqDTO transFerReqDTO = convertToAccountAllReqDTO(balanceCommand);

            log.info("日志跟踪号:{} 调账务转账接口参数:{}",  balanceCommand.getTraceLogId(),transFerReqDTO);
            AccountRespBO accountResult = accountService.transfer(balanceCommand.getPayNo(),
                    balanceCommand.getServiceCode(), transFerReqDTO);
            log.info("调账务转账接口响应结果 :{}", accountResult);

            CommandResult payResult = new CommandResult();
            BO2BOConverter.buildCommandResult(payResult, accountResult);

            payResult.setCommand(balanceCommand);
            result.put(balanceCommand.getCommandNo(), payResult);
        } catch (Exception e) {
            log.error("账务指令处理失败", e);
            CommandResult payResult = new CommandResult();
            payResult.setRealRtnFlag(true);
            payResult.setStatus(PayStatusEnums.FAIL.getCode());
            payResult.setResultCode(PayCoreErrorCode.REQUEST_ACCOUNT_ERROR.getErrorCode());
            payResult.setResultMsg(PayCoreErrorCode.REQUEST_ACCOUNT_ERROR.getErrorDesc());
            result.put(balanceCommand.getCommandNo(), payResult);
        }
        return result;
    }

    /**
     * 执行取消方法
     *
     * @param commands 支付命令
     * @return map          指令执行结果
     */
    @Override
    public Map<String, CommandResult> doCancel(List<BalanceCommand> commands) {
        return null;
    }

    /**
     * 转账务转账参数
     *
     * @param  balanceCommand 余额指令
     * @return                账务转账参数
     */
    private TransFerReqDTO convertToAccountAllReqDTO(BalanceCommand balanceCommand) {

        TransFerReqDTO accountAllReqDTO = new TransFerReqDTO();
        // 请求流水号
        accountAllReqDTO.setRequestNo(balanceCommand.getRequestBizNo());
        // 收款方
        accountAllReqDTO.setPayeeAccNo(balanceCommand.getPayeeAccNo());
        // 付款方
        accountAllReqDTO.setPayerAccNo(balanceCommand.getPayerAccNo());
        // 渠道大类
        accountAllReqDTO.setFundingChannel(balanceCommand.getFundingChannel());
        // 渠道细类
        accountAllReqDTO.setSubFundingChannel(balanceCommand.getSubFundingChannel());
        // 业务类
        accountAllReqDTO.setBizType(balanceCommand.getBizType());
        // 业务细类
        accountAllReqDTO.setSubBizType(balanceCommand.getSubBizType());
        // 请求系统
        accountAllReqDTO.setRequestSystem(RequestSystemEnum.PAYCORE.getCode());
        // 操作人
        accountAllReqDTO.setOperator(balanceCommand.getOperator());
        // 转账金额
        accountAllReqDTO.setAmount(balanceCommand.getBizChargeAmt());
        // 币种
        accountAllReqDTO.setCcy(balanceCommand.getCcy());
        // 请求时间
        accountAllReqDTO.setRequestDate(balanceCommand.getRequestDate());
        // 备注
        accountAllReqDTO.setRemark(balanceCommand.getRemark());
        // 关联No
        accountAllReqDTO.setRelationNo(balanceCommand.getRelationNo());
        // 事件号
        accountAllReqDTO.setEventNo(balanceCommand.getEventNo());
        //同步入账标识设置为同步
        accountAllReqDTO.setSyncPayeeFlag(balanceCommand.isAsyPayeeFlag());

        return accountAllReqDTO;
    }

}
